#--*-- coding:utf-8 --*--

import sys
import os
import os.path
import platform

from pyqt4_pyside import *
    
class SettingsPage(QtGui.QWidget):
    def __init__(self,  parent=None):
        super(SettingsPage,  self).__init__(parent)

        self.addPathButton = QtGui.QPushButton("Add...")
        self.addMunroPathButton = QtGui.QPushButton("Add Munro...")
        self.removePathButton = QtGui.QPushButton("Remove")
        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(0)
        vbox.addWidget(self.addPathButton)
        vbox.addWidget(self.addMunroPathButton)
        vbox.addWidget(self.removePathButton)
        
        self.readSettings()
        #note: frozen 
        #currentPath = os.path.dirname(os.path.abspath(__file__))
        #if not currentPath in self.pathList:
        #    self.pathList.insert(0,  currentPath)
        self.pathListView = QtGui.QListView()
        self.pathListModel = QtGui.QStringListModel(self.pathList)
        self.pathListView.setModel(self.pathListModel)
        
        hbox = QtGui.QHBoxLayout(self)
        hbox.addWidget(self.pathListView)
        hbox.addLayout(vbox)
                
        #self.setStyleSheet("background-color: rgb(200, 85, 0);")
        self.addPathButton.clicked.connect(self.onAddButtonClicked)
        self.addMunroPathButton.clicked.connect(self.onAddMunroButtonClicked)
        self.removePathButton.clicked.connect(self.onRemoveButtonClicked)
        self.pathListModel.dataChanged.connect(self.onListModelChanged)
        self.pathListModel.rowsRemoved.connect(self.onListModelRowsRemoved)
        self.updateEnviron()
        
    def onAddButtonClicked(self):
        dir = QtGui.QFileDialog.getExistingDirectory(self, u"选择路径")
        if dir:
            self.insertOneRow(dir)
            
    def insertOneRow(self,  path):
        self.pathListModel.insertRows(0,  1)
        self.pathListModel.setData(self.pathListModel.index(0),  path)
        
    def onAddMunroButtonClicked(self):
        munrodir = QtGui.QFileDialog.getExistingDirectory(self, u"选择Munro路径")
        if munrodir:
            #munrodir = munrodir.encode(sys.getfilesystemencoding( ))
            self.insertOneRow(os.path.join(munrodir, 'sofem', 'bin'))
            self.insertOneRow(os.path.join(munrodir, 'optics', 'bin'))
            self.insertOneRow(os.path.join(munrodir, 'source', 'bin'))
            self.insertOneRow(os.path.join(munrodir, 'refine', 'bin'))
        
    def onRemoveButtonClicked(self):
        if self.pathListView.currentIndex().isValid():
            row = self.pathListView.currentIndex().row()
            self.pathListModel.removeRows(row,  1,  QtCore.QModelIndex())
        
    def onListModelChanged(self, topLeft,  bottomRight):
        self.updateEnviron()
        
    def updateEnviron(self):
        binpath = self.pathListModel.stringList()
        binpath = [p.encode(sys.getfilesystemencoding( )) for p in binpath]
        binpath.append(os.environ['PATH'])
        if platform.system() == 'Windows':
            os.environ['PATH']= ';'.join(binpath)
        else:
            os.environ['PATH']= ':'.join(binpath)
        print os.environ['PATH']
        
    def onListModelRowsRemoved(self,  parent,  start,  end):
        self.updateEnviron()
        
    def readSettings(self):
        settings = QtCore.QSettings()
        settings.beginGroup("munropath")
        self.pathList = settings.value("paths",  [])
        if not isinstance(self.pathList,  list):
            self.pathList = []
        settings.endGroup()
        
    def writeSettings(self):
        settings = QtCore.QSettings()
        settings.beginGroup("munropath")
        settings.setValue("paths",  self.pathListModel.stringList())
        settings.endGroup()
